package org.nowcoder.leetcode.BTree;

/**
 * Title  : 404. Sum of Left Leaves
 * Source : https://leetcode.cn/problems/sum-of-left-leaves/description/
 * Author : XrazYang
 * Date   : 2023-04-15
 */

public class LeetCode_404 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    private int dfs(TreeNode root, boolean flag) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null && flag) {
            return root.val;
        }
        return dfs(root.left, true) + dfs(root.right, false);
    }

    public int sumOfLeftLeaves(TreeNode root) {
        return dfs(root, true);
    }
}
